// pages/diagram/diagram.ts
import * as echarts from '../../ec-canvas/echarts';
const chartsOption = require('../../utils/chartsOption')
var { request } = getApp();

function getMonthString(){
  return new Date().getMonth()+1>9?(new Date().getMonth()+1):
  ('0'+(new Date().getMonth()+1))
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:'month',
    monthList:[] as any,
    yearList:[] as any,
    assignList:[] as any,
    currentMonth:new Date().getMonth()+1+'月',
    currentYear:new Date().getFullYear()+'年',
    ec: {
      // onInit: initChart
      lazyLoad: true
    },
    barComponent:null as any,
    params:{
      assignee:'',
      assigneeName:''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.initMonthList()
    this.initYearList()
    this.initAssignList()
    this.initData({
      month:getMonthString()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  initMonthList(){
    let currentMonth = new Date().getMonth() + 1;
    console.log(currentMonth,'currentMonth');
    for(let i=currentMonth;i>=1;i--){
      this.data.monthList.push(`${i}月`)
    }
    console.log(this.data.monthList,'this.data.monthList');
    this.setData({
      monthList:this.data.monthList
    })
  },
  initYearList(){
    let currentYear =  new Date().getFullYear();
    console.log(currentYear,'currentYear');
    for(let i=currentYear;i>=2020;i--){
      this.data.yearList.push(`${i}年`)
    }
    console.log(this.data.yearList,'this.data.yearList');
    this.setData({
      yearList:this.data.yearList
    })
  },
  onChangeCard(event:any){
    console.log(event,'onChangeCard');
    this.setData({
      active:event.detail.name,
      currentMonth:new Date().getMonth()+1+'月',
      currentYear:new Date().getFullYear()+'年'
    })
    let params
    if(event.detail.name=='year'){
      params={
        year: new Date().getFullYear(),
        assignee:this.data.params.assignee
      }
    }else{
      params={
        month: getMonthString(),
        assignee:this.data.params.assignee
      }
    }
    this.initData(params)
  },
  initData(params:any){
    request('/admin/task/countTask', {
      method: 'POST',
      data: params
    }).then((res: any) => {
      console.log(res.data.data,'countTask');
      let result = res.data.data
      if (res.data.code == 1) {
        this.initChart({
          finish:result.completedTasks,
          unfinish:result.incompleteTasks,
          finishRate:result.completedTasksRatio,
          unfinishRate:result.incompleteTasksRatio,
        })
      }
    })
  },
  initChart(barData:any) {
    // 绑定组件
    this.data.barComponent = this.selectComponent("#mychart-dom-bar");
    // 初始化柱状图
    this.data.barComponent.init((canvas, width, height, dpr) => {
      // 初始化图表
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // 解决模糊显示问题
      })
      // 开发中根据从后端获取barData数据,动态更新图表
      setTimeout(()=>{
        chart.setOption(chartsOption.barOption(barData))
      },200)
      // return chart
    })
  },
  assignChange: function (e) {
    console.log(e.detail.value,'assignChange')
    this.setData({
      'params.assignee':e.detail.value,
      'params.assigneeName':this.data.assignList[e.detail.value].name
    })
    let params
    if(this.data.active=='year'){
      params={
        year: this.data.currentYear.slice(0,-1),
        assignee:e.detail.value
      }
    }else{
      let month = Number(this.data.currentMonth.slice(0,-1))
      params={
        month: month>9?month:'0'+month,
        assignee:e.detail.value
      }
    }
    this.initData(params)
  },
  initAssignList() {
    request('/admin/employee/page?page=1&pageSize=100', {
    }).then((res: any) => {
      if (res.data.code == 1) {
        let list = res.data.data.records
        this.setData({
          assignList: list.filter((item: any) => 
          item.processStatus == 1&&item.status==1),
        })
        console.log(this.data.assignList, 'assignList');
      }
    })
  },
  onChangeMonth(event:any){
    console.log(event.detail.name,'onChangeMonth');
    this.setData({
      currentMonth:event.detail.name
    })
    let month = event.detail.name.slice(0, -1)
    this.initData({
      month:month>9?month:'0'+month,
      assignee:this.data.params.assignee
    })
  },
  onChangeYear(event:any){
    console.log(event,'onChangeYear');
    this.setData({
      currentYear:event.detail.name
    })
    this.initData({
      year:event.detail.name.slice(0, -1),
      assignee:this.data.params.assignee
    })
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})